// src/api/roleAPI.js
import http from '@/utils/http'

/**
 * 添加系统角色
 * @param {Object} params - 角色信息对象
 */
export const addRoleAPI = (params) => {
  return http.post('/sys-client/cms/sysRole/add', params)
}

/**
 * 修改系统角色资料
 * @param {Object} params - 包含 id 和角色信息的对象
 */
export const updateRoleAPI = (params) => {
  return http.post(`/sys-client/cms/sysRole/update?id=${params.id}`, params.saveBO)
}

/**
 * 切换角色状态（0->1, 1->0）
 * @param {number} id - 角色ID
 */
export const toggleRoleStatusAPI = (id) => {
  return http.post(`/sys-client/cms/sysRole/toggleStatus?id=${id}`)
}

/**
 * 删除单个系统角色
 * @param {number} id - 角色ID
 */
export const deleteRoleAPI = (id) => {
  return http.post(`/sys-client/cms/sysRole/delete?id=${id}`)
}

/**
 * 批量删除系统角色
 * @param {string} ids - 以逗号分隔的ID字符串
 */
export const deleteBatchRoleAPI = (ids) => {
  return http.post(`/sys-client/cms/sysRole/deleteBatch?ids=${ids}`)
}

/**
 * 根据ID获取角色详情
 * @param {number} id - 角色ID
 */
export const getRoleByIdAPI = (id) => {
  return http.post(`/sys-client/cms/sysRole/get?id=${id}`)
}

/**
 * 获取所有系统角色列表（带分页）
 * @param {Object} params - 查询参数
 */
export const listAllRolesAPI = (params) => {
  console.log('🚀 发送请求:', {
    url: '/sys-client/cms/sysRole/listAll',
    method: 'post',
    data: params
  })
  return http.post('/sys-client/cms/sysRole/listAll', params).then(res => {
    console.log('✅ 接收到响应:', res)
    return res
  }).catch(err => {
    console.error('❌ 请求失败:', err)
    throw err
  })
}

/**
 * 获取所有状态启用的权限列表（不分页）
 * 不传参
 */
export const listAllNoPageAPI = () => {
  return http.post('/sys-client/cms/sysPermission/listAllNoPage')
}

/**
 * 为角色分配权限
 * @param {number} roleId - 角色ID
 * @param {Array} permissionIds - 权限ID列表
 */
export const assignPermissionsAPI = (roleId, permissionIds) => {
  return http.post('/sys-client/cms/sysRole/assign/permissions', permissionIds, {
    params: {
      roleId: roleId
    }
  })
}

/**
 * 解除角色和权限的绑定
 * @param {number} roleId - 角色ID
 * @param {number} permissionId - 权限ID
 */
export const unassignPermissionAPI = (roleId, permissionId) => {
  return http.post('/sys-client/cms/sysRole/assign/unassignPermission', {}, {
    params: {
      roleId: roleId,
      permissionId: permissionId
    }
  })
}

/**
 * 根据角色名模糊查询角色列表
 * @param {Object} params - 查询参数对象
 */
export const searchByRoleNameAPI = (params) => {
  return http.post('/sys-client/cms/sysRole/searchByRoleName', params, {
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

